package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单
 *
 * @author Chengfei
 * @date 2020/08/18
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 查询所有菜单
     *
     * @return List<Menu> 菜单集合
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Menu> menuList = menuService.findAll();
        if (menuList != null && menuList.size() > 0) {
            return new Result(true, "查询菜单成功", menuList);
        }

        return new Result(false, "查询菜单失败");
    }

    /**
     * 根据用户名查询相应的菜单
     *
     * @return List<Menu> 用户具有的菜单集合
     */
    @RequestMapping("/loadMenu")
    public Result loadMenu(String username) {
        try {
            //根据用户名查询菜单
            List<Menu> list = menuService.loadMenu(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }

    }

    /**
     * 分页查询  可带条件
     *
     * @param queryPageBean 封装 当前页码 每页记录数  查询条件
     * @return PageResult  封装  总条数  当前页面结果
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return menuService.findPage(queryPageBean);
    }

    /**
     * 添加菜单
     *
     * @param menu 菜单信息
     * @return true or false
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {

        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }

    }

    /**
     * 根据id查询菜单
     *
     * @param id 菜单id
     * @return Menu 菜单
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }

    }

    /**
     * 编辑菜单
     *
     * @param menu 菜单信息
     * @return true or false
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {

        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }

    }

    /**
     * 根据id删除菜单
     *
     * @param id 菜单id
     * @return true or false
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }

    }


}
